import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import { type AuthUser, type AuthToken } from "@/components/layout/types";

interface AuthState {
  auth: {
    user: AuthUser | null;
    token: AuthToken | null;
  };
  setUser: (user: AuthUser | null) => void;
  setToken: (token: AuthToken | null) => void;
}

export const useAuthStore = create<AuthState>()(
  persist(
    (set) => ({
      auth: {
        user: null,
        token: null,
      },
      setUser: (user) =>
        set((state) => ({ ...state, auth: { ...state.auth, user } })),
      setToken: (token) =>
        set((state) => ({ ...state, auth: { ...state.auth, token } })),
    }),
    {
      name: "auth",
      storage: createJSONStorage(() => localStorage), // (optional) by default, 'localStorage' is used
    }
  )
);

// export const useAuth = () => useAuthStore((state) => state.auth)
